//
//  HttpBaseModel.m
//  SinaWeibo
//
//  Created by Stephy_xue on 13-9-26.
//  Copyright (c) 2013年 Stephy_xue. All rights reserved.
//

#import "HttpBaseModel.h"

@implementation HttpBaseModel

static HttpBaseModel* mInstance;
static HttpBaseModel* AnothermInstance;

-(id)init
{
    self = [super init];
    if(self != nil)
    {
        self.engine = [[MKNetworkEngine alloc] initWithHostName:HeadUrl];
    }
    
    return self;
}

-(id)initAnotherplatform
{
    self = [super init];
    if(self != nil)
    {
        self.Anotherengine = [[MKNetworkEngine alloc] initWithHostName:YOUHUASHUOUrl];
    }
    
    return self;
}

+ (HttpBaseModel*)getInstance
{
    if (nil == mInstance)
    {
        mInstance = [[HttpBaseModel alloc] init];
        
    }
    
    return mInstance;
}

+ (HttpBaseModel*)getAnotherInstance
{
    if (nil == AnothermInstance)
    {
        AnothermInstance = [[HttpBaseModel alloc] initAnotherplatform];
        
    }
    
    return AnothermInstance;
}
-(void)getDataResponsePath:(NSString *)path
                    params:(NSMutableDictionary*) params
                httpMethod:(NSString*)method
              onCompletion:(void (^)(NSData *responseData))completionBlock
                   onError:(MKNKErrorBlock)errorBlock
{
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:params,@"param",nil];
    MKNetworkOperation *op =[self.engine operationWithPath:path params:param httpMethod:method ssl:NO];
    [op addCompletionHandler:^(MKNetworkOperation *operation){
        NSData *responseData = [operation responseData];
        completionBlock(responseData);
    } errorHandler:^(MKNetworkOperation* completedOperation, NSError* error){
        errorBlock(error);
    }];
    [self.engine enqueueOperation:op];
}

-(void)getAnotherDataResponsePath:(NSString *)path
                           params:(NSMutableDictionary*) params
                       httpMethod:(NSString*)method
                     onCompletion:(void (^)(NSData *responseData))completionBlock
                          onError:(MKNKErrorBlock)errorBlock
{
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:params,@"param",nil];
    MKNetworkOperation *op =[self.Anotherengine operationWithPath:path params:param httpMethod:method ssl:NO];
    [op addCompletionHandler:^(MKNetworkOperation *operation){
        NSData *responseData = [operation responseData];
        completionBlock(responseData);
    } errorHandler:^(MKNetworkOperation* completedOperation, NSError* error){
        errorBlock(error);
    }];
    [self.Anotherengine enqueueOperation:op];
}



//+ (void)getDataResponseHostName:(NSString *)hostName
//                           Path:(NSString *)path
//                         params:(NSMutableDictionary*) params
//                     httpMethod:(NSString*)method
//                   onCompletion:(void (^)(NSData *responseData))completionBlock
//                        onError:(MKNKErrorBlock)errorBlock
//{
//    MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:hostName];
//
//    MKNetworkOperation *op =[engine operationWithPath:path params:params httpMethod:method ssl:NO];
//
//    [op addCompletionHandler:^(MKNetworkOperation *operation){
//        NSData *responseData = [operation responseData];
//        completionBlock(responseData);
//    } errorHandler:^(MKNetworkOperation* completedOperation, NSError* error){
//        errorBlock(error);
//    }];
//
//    [engine enqueueOperation:op];
//}

- (void)uploadImageHostName:(NSString *)path
                      image:(NSData *)imageData
                     params:(NSMutableDictionary*) params
                 httpMethod:(NSString*)method
               onCompletion:(void (^)(NSData *responseData))completionBlock
                    onError:(MKNKErrorBlock)errorBlock
{
    MKNetworkOperation *op =[self.engine operationWithPath:path params:params httpMethod:method ssl:NO];
    [op addData:imageData forKey:[params objectForKey:@"key"]];
    [op addCompletionHandler:^(MKNetworkOperation *operation){
        NSData *responseData = [operation responseData];
        completionBlock(responseData);
    } errorHandler:^(MKNetworkOperation* completedOperation, NSError* error){
        errorBlock(error);
    }];
    
    [self.engine enqueueOperation:op];
}
- (void)AnotheruploadImageHostName:(NSString *)path
                             image:(NSData *)imageData
                            params:(NSMutableDictionary*) params
                        httpMethod:(NSString*)method
                      onCompletion:(void (^)(NSData *responseData))completionBlock
                           onError:(MKNKErrorBlock)errorBlock
{
    MKNetworkOperation *op =[self.Anotherengine operationWithPath:path params:params httpMethod:method ssl:NO];
    [op addData:imageData forKey:[params objectForKey:@"key"]];
    [op addCompletionHandler:^(MKNetworkOperation *operation){
        NSData *responseData = [operation responseData];
        completionBlock(responseData);
    } errorHandler:^(MKNetworkOperation* completedOperation, NSError* error){
        errorBlock(error);
    }];
    
    [self.Anotherengine enqueueOperation:op];
}

@end
